package com.huawei.esdk.tp.northcommu.cxf.professional.convert;

import java.util.ArrayList;
import java.util.List;

import com.huawei.esdk.platform.common.SDKResult;
import com.huawei.esdk.platform.common.exception.SDKException;
import com.huawei.esdk.tp.common.ErrInfo;
import com.huawei.esdk.tp.domain.model.bean.MCUInfo;
import com.huawei.esdk.tp.domain.model.bean.MCUInfoList;
import com.huawei.esdk.tp.domain.model.bean.MCUResource;
import com.huawei.esdk.tp.domain.model.bean.MCUResourceItem;
import com.huawei.esdk.tp.domain.model.bean.MCUResourceList;
import com.huawei.esdk.tp.domain.model.bean.PagesInfo;
import com.huawei.esdk.tp.domain.model.bean.QueryCondition;
import com.huawei.esdk.tp.domain.model.bean.QuerySites;
import com.huawei.esdk.tp.domain.model.bean.ResourceOccupiedStatus;
import com.huawei.esdk.tp.domain.model.bean.SortField;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.DoubleFilterEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.FilterBaseEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.IntFilterEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.MCUInfoEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.MCUResourceEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.MCUResourceItemEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.PageParamEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.PagesInfoEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.QueryConfigEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.QueryMCUFutureResourceExResponse;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.QueryMCUInfoEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.QueryMCUInfoExResponse;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.ResourceOccupiedStatusEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.SortItemEx;
import com.huawei.esdk.tp.northcommu.cxf.professional.gen.StringFilterEx;

public class TPProfessionalMCUMgrConvert
{
    
    public QuerySites queryMCUInfoExSoap2Modal(QueryMCUInfoEx parameters)
        throws SDKException
    {
        QuerySites querySites = new QuerySites();
        
        // 进入转换方法前，必填参数已做过校验
        QueryConfigEx queryConfig = parameters.getQueryConfig();
        
        PageParamEx pageInfo = queryConfig.getPageParam();
        
        querySites.setCurrentPage(pageInfo.getCurrentPage());
        querySites.setNumberPerPage(pageInfo.getNumberPerPage());
        
        List<SortItemEx> sortItems = queryConfig.getSortItems();
        if (!sortItems.isEmpty())
        {
            List<SortField> sortItemsModal = new ArrayList<SortField>();
            for (SortItemEx stItem : sortItems)
            {
                Integer itemIndex = stItem.getItemIndex();
                Integer sort = stItem.getSort();
                
                // DTS2015012305074 
                // 非必填参数不为空时，校验其必填属性
                if (null == itemIndex || null == sort)
                {
                    SDKException sdkException = new SDKException(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORDESC);
                    sdkException.setSdkErrCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                    throw sdkException;
                }
                
                SortField sortField = new SortField();
                sortField.setIsAscend(sort);
                sortField.setFieldIndex(itemIndex);
                sortItemsModal.add(sortField);
            }
            querySites.setSortField(sortItemsModal);
        }
        
        List<FilterBaseEx> filters = queryConfig.getFilters();
        if (!filters.isEmpty())
        {
            List<QueryCondition> queryConditions = new ArrayList<QueryCondition>();
            for (FilterBaseEx ftItem : filters)
            {
                Integer columnIndex = ftItem.getColumnIndex();
                
                // DTS2015012305074 
                // 非必填参数不为空时，北向转换校验其必填属性
                if (null == columnIndex)
                {
                    SDKException sdkException = new SDKException(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORDESC);
                    sdkException.setSdkErrCode(ErrInfo.SDK_PARAM_NOT_COMPLETE_ERRORCODE);
                    throw sdkException;
                }
                
                QueryCondition queryCondition = getQueryConditionSoap2Modal(ftItem);
                if (null != queryCondition)
                {
                    queryConditions.add(queryCondition);
                }
            }
            querySites.setQueryCondition(queryConditions);
        }
        
        if (null != queryConfig.getFocusItem())
        {
            querySites.setFocusItem((long)queryConfig.getFocusItem());
        }
        
        return querySites;
        
    }
    
    private QueryCondition getQueryConditionSoap2Modal(FilterBaseEx filtersEx)
    {
        if (StringFilterEx.class.isInstance(filtersEx))
        {
            StringFilterEx filtersEx1 = (StringFilterEx)filtersEx;
            QueryCondition queryCondition = new QueryCondition();
            queryCondition.setConditionName("StringFilterEx");
            queryCondition.setConditionType("1");
            queryCondition.setConditionValue(new Object[] {filtersEx1.getColumnIndex(), filtersEx1.getValue()});
            return queryCondition;
        }
        else if (IntFilterEx.class.isInstance(filtersEx))
        {
            IntFilterEx filtersEx1 = (IntFilterEx)filtersEx;
            QueryCondition queryCondition = new QueryCondition();
            queryCondition.setConditionName("IntFilterEx");
            queryCondition.setConditionType("2");
            queryCondition.setConditionValue(new Object[] {filtersEx1.getColumnIndex(), filtersEx1.getGreaterThan(),
                filtersEx1.getLessThan(), filtersEx1.getEqualTo()});
            return queryCondition;
        }
        else if (DoubleFilterEx.class.isInstance(filtersEx))
        {
            DoubleFilterEx filtersEx1 = (DoubleFilterEx)filtersEx;
            QueryCondition queryCondition = new QueryCondition();
            queryCondition.setConditionName("DoubleFilterEx");
            queryCondition.setConditionType("3");
            queryCondition.setConditionValue(new Object[] {filtersEx1.getColumnIndex(), filtersEx1.getGreaterThan(),
                filtersEx1.getLessThan(), filtersEx1.getEqualTo()});
            return queryCondition;
        }
        else
        {
            return null;
        }
        
    }
    
    public QueryMCUInfoExResponse queryMCUInfoExModal2Soap(SDKResult<MCUInfoList> result)
    {
        QueryMCUInfoExResponse response = new QueryMCUInfoExResponse();
        response.setResultCode((int)result.getErrCode());
        
        MCUInfoEx mcuInfoEx = null;
        
        List<MCUInfo> mcuInfos = result.getResult().getMcuInfos();
        if (null != mcuInfos && !mcuInfos.isEmpty())
        {
            for (MCUInfo mcuInfo : mcuInfos)
            {
                mcuInfoEx = new MCUInfoEx();
                mcuInfoEx.setDomainName(mcuInfo.getDomainName());
                mcuInfoEx.setId(mcuInfo.getId());
                mcuInfoEx.setIp1(mcuInfo.getIp1());
                mcuInfoEx.setIp2(mcuInfo.getIp2());
                mcuInfoEx.setName(mcuInfo.getName());
                
                if (mcuInfo.getPort1() != null)
                {
                    mcuInfoEx.setPort1(Integer.parseInt(String.valueOf(mcuInfo.getPort1())));
                }
                
                if (mcuInfo.getPort2() != null)
                {
                    mcuInfoEx.setPort2(Integer.parseInt(String.valueOf(mcuInfo.getPort2())));
                }
                
                mcuInfoEx.setStutas(mcuInfo.getStatus());
                mcuInfoEx.setType(mcuInfo.getType());
                
                response.getMcus().add(mcuInfoEx);
            }
        }
        
        PagesInfoEx pageInfo = new PagesInfoEx();
        PagesInfo pagesInfo = result.getResult().getPagesInfo();
        if (null != pagesInfo)
        {
            pageInfo.setCurrentPage(pagesInfo.getCurrentPage());
            pageInfo.setTotalPages(pagesInfo.getTotalPages());
            pageInfo.setTotalRecords(pagesInfo.getTotalRecords());
        }
        
        response.setPageInfo(pageInfo);
        
        return response;
    }
    
    public QueryMCUFutureResourceExResponse queryMCUFutureResourceExModal2Soap(SDKResult<MCUResourceList> result)
    {
        QueryMCUFutureResourceExResponse response = new QueryMCUFutureResourceExResponse();
        response.setResultCode((int)result.getErrCode());
        MCUResourceList resultList = result.getResult();
        if (null == resultList)
        {
            return response;
        }
        List<MCUResource> mcuResources = resultList.getMcuResource();
        if (null != mcuResources && !mcuResources.isEmpty())
        {
            MCUResourceEx mcuResListEx = null;
            
            for (MCUResource mcuResource : mcuResources)
            {
                mcuResListEx = new MCUResourceEx();
                mcuResListEx.setMcuId(mcuResource.getMcuId());
                
                List<MCUResourceItem> resourceList = mcuResource.getResourceList();
                if (null != resourceList && !resourceList.isEmpty())
                {
                    MCUResourceItemEx resourceListEx = null;
                    
                    for (MCUResourceItem mcuResourceItem : resourceList)
                    {
                        resourceListEx = new MCUResourceItemEx();
                        resourceListEx.setResourceType(mcuResourceItem.getResourceType());
                        
                        List<ResourceOccupiedStatus> resourceStatus = mcuResourceItem.getResourceStatus();
                        if (null != resourceStatus && !resourceStatus.isEmpty())
                        {
                            ResourceOccupiedStatusEx resourceStatusEx = null;
                            
                            for (ResourceOccupiedStatus resourceOccupiedStatus : resourceStatus)
                            {
                                resourceStatusEx = new ResourceOccupiedStatusEx();
                                resourceStatusEx.setBeginTime(resourceOccupiedStatus.getBeginTime());
                                resourceStatusEx.setDuration(resourceOccupiedStatus.getDuration());
                                resourceStatusEx.setFreeCount(resourceOccupiedStatus.getFreeCount());
                                resourceStatusEx.setTotalCount(resourceOccupiedStatus.getTotalCount());
                                
                                resourceListEx.getResourceStatus().add(resourceStatusEx);
                            }
                        }
                        
                        mcuResListEx.getResourceList().add(resourceListEx);
                    }
                }
                
                response.getMcuResList().add(mcuResListEx);
            }
        }
        
        return response;
    }
    
}
